// pages/ticket-info/ticket-info.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    buyLeft:0,
    getList:[],
    goodsList:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showShareMenu({
      withShareTicket: true
    })
    var that = this;
    wx.request({
      url: app.globalData.base_url + "Customer/getCustomerInfo",
      // port: app.globalData.base_port,
      header: {
        'Content-Type': 'application/json;charset=UTF-8;'
      },
      method: "POST",
      data: {
        "CUSTOMER_ID": wx.getStorageSync('user_id')
      },
      success: function (res) {
        var buyLeft = 0;
        if (res.data.data.TICKET_BUY_NUM_LEFT>0) {
          buyLeft = res.data.data.TICKET_BUY_NUM_LEFT;
        };
        var getList = res.data.data.free_ticket;
        that.setData({
          buyLeft: buyLeft,
          getList: getList
        });
      }
    }); 
    wx.request({
      url: app.globalData.base_url + "Goods/getGoodsList",
      // port: app.globalData.base_port,
      header: {
        'Content-Type': 'application/json;charset=UTF-8;'
      },
      method: "POST",
      data: {
        "CUSTOMER_ID": wx.getStorageSync('user_id')
      },
      success: function (res) {
        // console.log(res)
        that.setData({
         goodsList: res.data.data
        });
      }
    })
    
  },
  //分享
  onShareAppMessage: function (res) {
    let that = this
    return {
      title: '找苗木网',
      path: '/pages/login1/login1',
      imageUrl: '../../source/icon/banner.png',
      success: function (res) {
        console.log(res.shareTickets)
        //getSystemInfo是为了获取当前设备信息，判断是android还是ios，如果是android
        //还需要调用wx.getShareInfo()，只有当成功回调才是转发群，ios就只需判断shareTickets
        //获取用户设备信息
        wx.getSystemInfo({
          success: function (d) {
            console.log(d);
            if (res.shareTickets != undefined) {
              console.log("分享的是群");
              wx.getShareInfo({
                shareTicket: res.shareTickets[0],
                success: function (res) {
                  //分享到群之后你要做的事情
                  wx.request({
                    url: app.globalData.base_url + "Customer/grantTicketByGroup",
                    header: {
                      'Content-Type': 'application/json;charset=UTF-8;'
                    },
                    method: "POST",
                    data: {
                      "CUSTOMER_ID": wx.getStorageSync('user_id'),
                    },
                    complete: function (res) {
                      that.setData({

                      });
                      if (res == null || res.data == null) {
                        console.error('网络请求失败');
                        return;
                      }
                    },
                    success: function (res) {
                      if (res.data.code === "200") {
                        wx.showModal({
                          title: '提示',
                          content: '赠送您一张估价券哦，有效时间24小时',
                          success: function (res) {
                            if (res.confirm) {
                              console.log('用户点击确定')
                            } else if (res.cancel) {
                              console.log('用户点击取消')
                            }
                          }
                        })
                      } else if (res.data.code === "203") {
                        var maxGrantCount = res.data.data;
                        wx.showModal({
                          title: '提示',
                          content: '分享到群每天最多赠送 ' + maxGrantCount + ' 张您已达到上限',
                          success: function (res) {
                            if (res.confirm) {
                              console.log('用户点击确定')
                            } else if (res.cancel) {
                              console.log('用户点击取消')
                            }
                          }
                        })
                      }
                    }
                  })
                }
              })
            } else {//分享到个人要做的事情，我给的是一个提示
              console.log("分享的是个人");
              wx.showModal({
                title: '提示',
                content: '分享到群赠送估价券哦',
                success: function (res) {
                  if (res.confirm) {
                    console.log('用户点击确定')
                  } else if (res.cancel) {
                    console.log('用户点击取消')
                  }
                }
              })
            }
          },
          fail: function (res) {

          }
        })
      }

    }
  },

  buyTicket: function(e) {
    var goodsId = e.target.dataset.value;
    var that = this
    wx.login({
      success: function (res) {
        // 成功的话会返回：
        // {errMsg: "login:ok", code: "获取用户OpenID的ticket"}
        that.getOpenId(res.code, goodsId)
      }
    })
  },

  getOpenId: function (jsCode, goodsId) {
    var that = this
    wx.request({
      url: app.globalData.base_url + "login/getTicket1",
      header: {
        'Content-Type': 'application/json;charset=UTF-8;'
      },
      method: "POST",
      data: {
        code: jsCode // wx.login()时得到的ticket
      },
      success: function (res) {
        that.getPrePayId(res.data.data.openid, goodsId)
      }
    })
  },

// 统一下单
  getPrePayId: function (openId, goodsId) {
    var that = this
    wx.request({
      url: app.globalData.base_url + "Buy/buyTicket",
      header: {
        'Content-Type': 'application/json;charset=UTF-8;'
      },
      method: "POST",
      data: {
        openid: openId,
        GOODS_ID: goodsId,
        CUSTOMER_ID: wx.getStorageSync('user_id'),
        
      },
      success: function (res) {
        console.log(res)
        that.pay(res.data.data, goodsId)
      }
    })
  },

  // data是服务端返回的JSON
  // 加上success、fail回调后，正好符合wx.requestPayment()参数的格式

  pay: function (data, goodsId) {
    var that = this;
    data.success = function (res) {
      // 用户支付成功后的代码
      // 插入购买记录
      wx.request({
        url: app.globalData.base_url + "Buy/insertBuyTicketFreeRecord",
        header: {
          'Content-Type': 'application/json;charset=UTF-8;'
        },
        method: "POST",
        data: {
          GOODS_ID: goodsId,
          CUSTOMER_ID: wx.getStorageSync('user_id'),
        },
        success: function (res) {
          // console.log(res);
          if (res.data.code === '200') {
            that.setData({
              buyLeft: res.data.data
            })
          }      
        }
      })
    }
    data.fail = function (res) {
      // 用户支付失败后的代码
    }
    wx.requestPayment(data)
  }

})